//
//  LZMsgCenterPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/13.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZMsgCenterPresenter.h"
#import "NetworkServiceManager.h"
#import "MJExtension.h"
#import "NSString+Additions.h"
#import "LZUserModel.h"

#import "LZMessageModel.h"
#import "LAReviewModel.h"

extern NSString *customer;
static NSString *const kMsgListURL = @"/clientSellerMsg/listPage"; //消息列表
static NSString *const kHaveUnReadMsgURL = @"/msg/hasUnReadMsg"; //是否有未读消息
static NSString *const kChangeMsgStateURL = @"/clientSellerMsg/update"; // 更改消息未已读状态
static NSString *const kVideoReplyURL = @"/clientvideoComments/replyInfo"; // 视频评论回复
static NSString *const kRestaurantReplyURL = @"/clientrestaurantComments/replyInfo"; //餐厅评论回复
static NSString *const kDeleteMsgURL = @"/clientSellerMsg/delete"; //删除消息

@implementation LZMsgCenterPresenter

#pragma mark - 消息列表
- (void)getMsgListWithPage:(NSInteger)page Limit:(NSInteger)limit RemoveCount:(NSInteger)rCount Success:(void (^)(NSArray *, NSInteger, NSInteger))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(customer, kMsgListURL);
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:[NSNumber numberWithInteger:page] forKey:@"cpage"];
    [dic setObject:[NSNumber numberWithInteger:limit] forKey:@"limit"];
    [dic setObject:[NSNumber numberWithInteger:rCount] forKey:@"removeCnt"];
    if ([LZUserModel Instance].userToken.length > 0) {
        [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    }
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            NSInteger totalPageTemp = [resultData[@"data"][@"totalPages"] integerValue];
            NSInteger currentPageTemp = [resultData[@"data"][@"cPageNO"] integerValue];
            NSMutableArray *modelArr = [[NSMutableArray alloc] init];
            NSArray *arr = resultData[@"data"][@"list"];
            for (NSInteger i = 0; i< arr.count; i ++) {
                LZMessageModel *m = [LZMessageModel mj_objectWithKeyValues:arr[i]];
                [modelArr addObject:m];
            }
            
            if (successBlock) {
                successBlock(modelArr,totalPageTemp,currentPageTemp);
            }
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark - 删除消息
- (void)removeMsgWithMsgId:(NSString *)msgId Success:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(customer,kDeleteMsgURL);
    NSDictionary *dic = @{@"id" : msgId};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            if (successBlock) {
                successBlock(resultData);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark - 更改消息状态为已读
- (void)changeMsgStateReadedWithMsgID:(NSString *)msgId Success:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    if (msgId.length == 0) {
        return;
    }
    NSString *urlStr = COMBINATION(customer,kChangeMsgStateURL);
    NSDictionary *dic = @{@"id" : msgId};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            if (successBlock) {
                successBlock(resultData);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark - 请求视频评论回复
- (void)getVideoReplyWithReplyID:(NSString *)replyId Success:(void(^)(LAReviewModel *model))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(customer,kVideoReplyURL);
    NSDictionary *dic = @{@"replyId" : replyId};
    
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            LAReviewModel *m = [LAReviewModel mj_objectWithKeyValues:resultData[@"data"]];
            if (successBlock) {
                successBlock(m);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
    
}

#pragma mark - 请求餐厅评论回复
- (void)getRestaurantReplyWithReplyID:(NSString *)replyId Success:(void(^)(LAReviewModel *model))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(customer,kRestaurantReplyURL);
    NSDictionary *dic = @{@"replyId" : replyId};
    
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            LAReviewModel *m = [LAReviewModel mj_objectWithKeyValues:resultData[@"data"]];
            if (successBlock) {
                successBlock(m);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];


}

@end
